package com.nyist.threadCase;

import cn.hutool.core.util.IdUtil;
import com.nyist.thread.NyistThreadUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 1.
 *
 * @ClassName MyThreadRunnable
 * @Description
 * @Author xiaoyang
 * @Date 2023/04/03/20:19
 */
@Slf4j
public class MyThreadRunnable3 implements Runnable {

    static volatile int ticket = 100;
    Lock lock = new ReentrantLock();

    @Override
    public void run() {
        // 线程执行的代码
        while (true) {
            lock.lock();
            if (ticket > 0) {
                NyistThreadUtils.sleep(1000);
                log.info("=====>>>ThreadName:{} , 卖了第 {} 张票, 编码为:{}", NyistThreadUtils.getThreadName(), ticket--, IdUtil.fastSimpleUUID());
            } else {
                log.error("=====>>>卖完了,当前线程为:{}", NyistThreadUtils.getThreadName());
                break;
            }
            lock.unlock();
        }
    }

}